package com.xywh.wanandroid.network;


import com.xywh.wanandroid.util.CacheUtil
import okhttp3.Interceptor
import okhttp3.Response
import org.json.JSONObject
import java.io.IOException


class LoginInterceptor : Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val response = chain.proceed(request)
        try {
            val responseBody = response.peekBody((1024 * 1024).toLong())
            val respString = responseBody.string()
            val `object` = JSONObject(respString)
            val errorCode = `object`.getString("code")
//            val data = `object`.getString("Data")
            if (errorCode == "401") {
//            if (respString.contains("请求未授权，拒绝访问")) {
                CacheUtil.setIsLogin(false)
//                        appViewModel?.userInfo?.value = null
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return response
    }
}
